package org.vnl.platform.impl.openjpa.util;

import java.util.Map;

import org.vnl.platform.impl.openjpa.orm.JPAConnectionManager;
import org.vnl.platform.impl.openjpa.orm.JPAPersistenceManger;
import org.vnl.platform.job.IJob;
import org.vnl.platform.job.JobExecException;
import org.vnl.platform.orm.PersistenceManagerFactory;


/**
 * 此Job是把利用JPA實作了PDK中的ORM的implementation作起始設定,接受了PERSISTE_UNIT_NAME及PERSISTENCE_MANAGER_NAME兩個參數
 * 
 * @author Vaughan
 *
 */
public class PersistenceManagerSetupJob implements IJob {

    private static final String _PERSISTE_UNIT_NAME = "PERSISTE_UNIT_NAME";
    private static final String _PERSISTENCE_MANAGER_NAME = "PERSISTENCE_MANAGER_NAME";
    
    private String puName;
    private String pmName;
    
    public PersistenceManagerSetupJob() {
        puName = "";
        pmName = "";
    }
    
    @Override
    public void setParams(Map<String, ? extends Object> params) {
        puName = params.get(_PERSISTE_UNIT_NAME).toString();
        pmName = params.get(_PERSISTENCE_MANAGER_NAME).toString();
    }

    @Override
    public int exec() throws JobExecException {
        JPAConnectionManager cm = new JPAConnectionManager();
        cm.setPersistUnit(puName);
        JPAPersistenceManger pm = new JPAPersistenceManger();
        pm.setConnectionManager(cm);
        PersistenceManagerFactory.getInstance().addPM(pmName, pm);
        return RESULT_SUCCESS;
    }

    @Override
    public void reset() throws JobExecException {
        this.puName = "";
        this.pmName = "";
    }

}
